package Or

import (
	"database/sql"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/ref"
	"strings"
)

func (pm ProcedureMe) Set(parameter string, value any) (err error) {
	return ref.UpdateStructJsonValue(pm, parameter, value)
}

func (pm ProcedureMe) Result(object string) global.Return {
	switch object {
	default:
		return global.Return{}
	}
}
func (pm ProcedureMe) Name(Con *sql.DB) (result global.Return, err error) {
	var event = "[Name]"
	var execSql []string
	pm.Parameter.Con = Con
	for _,v := range []string{"ALL_OBJECTS", "DBA_OBJECTS",}{
		execSql = append(execSql, fmt.Sprintf("select OBJECT_NAME from %v where  OBJECT_TYPE = 'PROCEDURE' AND STATUS != 'INVALID' AND OWNER ='%v' %v",
			v,pm.Parameter.Object.Schema, func() string {
				switch {
				case pm.Parameter.Object.Table == "*":
					return ""
				case strings.Contains(pm.Parameter.Object.Table, "%"):
					return fmt.Sprintf(" and OBJECT_NAME like '%%%v%%'", pm.Parameter.Object.Table)
				default:
					return fmt.Sprintf(" and OBJECT_NAME = '%v'", pm.Parameter.Object.Table)
				}
			}()))
	}
	if result, err = forExecQuerySql(event, pm.Parameter, ETL.StringGarth, execSql); err != nil || result.Result == nil {
		return
	}
	return
}
func (pm ProcedureMe) Existence(Con *sql.DB) (result global.Return, err error) {
	var event = "[Existence]"
	var execSql []string
	pm.Parameter.Con = Con
	for _,v := range []string{"ALL_OBJECTS", "DBA_OBJECTS",} {
		execSql = append(execSql, fmt.Sprintf("select OBJECT_NAME from %v where OBJECT_TYPE = 'PROCEDURE' AND STATUS != 'INVALID' and OWNER = '%v' and OBJECT_NAME='%v'",v, pm.Parameter.Object.Schema, pm.Parameter.Object.Table))
	}
	if result, err = forExecQuerySql(event, pm.Parameter, ETL.String, execSql); err != nil || result.Result == nil {
		return
	}
	switch {
	case strings.EqualFold(fmt.Sprintf("%v", result.Result), pm.Parameter.Object.Table):
		result.Result = true
	default:
		result.Result = false
	}
	return
}

func (pm ProcedureMe) ProcedureDefinition(Con *sql.DB) (result global.Return, err error) {
	return
}
